package com.skit.main.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.skit.common.ResourcesManager
import com.skit.common.bean.ActivityBean
import com.skit.common.manager.UserManager
import com.skit.main.R
import kotlinx.android.synthetic.main.main_fragment_activity_info.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since  2020/5/27
 */

class ActivityInfoFragment : Fragment() {
    private val format = SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault())
    lateinit var bean: ActivityBean

    companion object {
        fun newInstances(activityBean: ActivityBean): ActivityInfoFragment {
            val fragment: ActivityInfoFragment = ActivityInfoFragment()
            val bundle: Bundle = Bundle()
            bundle.putSerializable("bean", activityBean)

            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.main_fragment_activity_info, container, false)

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        bean = arguments?.getSerializable("bean") as ActivityBean
        data(bean)
    }


    public fun data(activityBean: ActivityBean) {
        this.bean = activityBean
        if (bean.organizer == null) return
        organizer.text =
            resources.getString(R.string.main_organizer, bean.organizer?.departmentName)
        event_location.text = ResourcesManager.getRes().getString(
            R.string.main_event_location, bean.eventLocation
        )

        activity_time.text = resources.getString(
            R.string.main_activity_time,
            format.format(bean.startTime),
            format.format(bean.endTime)
        )

        apply_time.text = resources.getString(
            R.string.main_apply_time,
            format.format(bean.signUpStartTime),
            format.format(bean.signUpEndTime)
        )

        activity_introduction.text = resources.getString(
            R.string.activity_introduction, bean.activityDescription
        )


        apply_num.text = resources.getString(
            R.string.main_xiegang,
            bean.registeredNum,
            bean.quotaNum
        )

        val parent = status_tv.parent as View;
        status_tv.text =
            if (bean.startTime > System.currentTimeMillis()) "待开始" else "已开始"
        if (bean.startTime < System.currentTimeMillis()) {
            parent.setBackgroundColor(
                Color.parseColor("#ff1E88E5")
            )
            status_tv.text = "已开始"
        } else {
            status_tv.text = "待开始"
        }
        if (bean.endTime < System.currentTimeMillis()) {
            parent.setBackgroundColor(
                ContextCompat.getColor(
                    requireActivity(),
                    R.color.gray
                )
            )
            status_tv.text = "已结束"
        }

        val range = bean.range
        val scoupe = when (range) {
            0 -> {
                "全校"
            }
            1 -> {
                val campus = bean.organizer!!.campus
                if (campus == 1) "东区" else if (campus == 2) "西区" else "未知"
            }
            2 -> {
                val id = UserManager.user?.userInfo?.department?.id
                bean.organizer!!.departmentName
            }
//            3 -> {
//                val id = UserManager.user?.userInfo?.department?.id
//                "aas"
//            }
            else -> "随缘"
        }

        application_scope.text = resources.getString(R.string.application_scope, scoupe)
    }
}